package com.example.demo.service.impl;

import com.example.demo.constant.MessageConstant;
import com.example.demo.context.BaseContext;
import com.example.demo.exception.LoginFailedException;
import com.example.demo.exception.PasswordErrorException;
import com.example.demo.exception.RegisterException;
import com.example.demo.mapper.ForumMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.mapper.UserSupportMapper;
import com.example.demo.pojo.PageResult;
import com.example.demo.pojo.dto.UserDetailDTO;
import com.example.demo.pojo.dto.UserLoginDTO;
import com.example.demo.pojo.dto.UserPageDTO;
import com.example.demo.pojo.dto.UserRegisterDTO;
import com.example.demo.pojo.entity.User;
import com.example.demo.pojo.vo.UserVO;
import com.example.demo.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserSupportMapper userSupportMapper;
    @Autowired
    private ForumMapper forumMapper;

    @Override
    public User login(UserLoginDTO userLoginDTO) {
        String username=userLoginDTO.getUsername();

        User user=userMapper.getByUsername(username);

        if(user==null){
            throw new LoginFailedException(MessageConstant.USER_UNREGISTERED);
        }

        String password=userLoginDTO.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(user.getPassword())){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        return user;
    }

    @Override
    public void register(UserRegisterDTO userRegisterDTO) {
        String username=userRegisterDTO.getUsername();

        User userDB=userMapper.getByUsername(username);
        if(userDB!=null){
            throw new RegisterException(MessageConstant.ACCOUNT_REGISTERED);
        }

        User user=new User();
        BeanUtils.copyProperties(userRegisterDTO,user);
        String password=userRegisterDTO.getPassword();
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password);
        user.setCreateTime(LocalDateTime.now());
        userMapper.insert(user);
    }

    @Override
    public void addDetail(UserDetailDTO userDetailDTO) {
        User user = new User();
        user.setId(BaseContext.getCurrentId());
        user.setVip(0);
        BeanUtils.copyProperties(userDetailDTO,user);
        userMapper.update(user);
    }

    @Override
    public PageResult page(UserPageDTO userPageDTO) {
        PageHelper.startPage(userPageDTO.getPage(), userPageDTO.getPageSize());
        Page<User> page=userMapper.pageQuery(userPageDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public UserVO userCenter() {
        UserVO userVO=new UserVO();
        Long userId=BaseContext.getCurrentId();
        User user=userMapper.getById(userId);
        userVO.setId(userId);
        userVO.setName(user.getName());
        userVO.setUsername(user.getUsername());
        userVO.setPcosOnsetDate(user.getPcosOnsetDate());
        LocalDateTime createTime=user.getCreateTime();
        LocalDate startDate= LocalDate.of(createTime.getYear(),createTime.getMonth(),createTime.getDayOfMonth());
        LocalDate endDate=LocalDate.now();
        Long diff= ChronoUnit.DAYS.between(startDate,endDate);
        userVO.setImprovementTime(diff);

        Long number=forumMapper.getNumberById(userId);
        userVO.setPosts(number);
        return userVO;

    }
}
